import useTokenStore from "@/stores/token"
import request, { wrap_api_func } from "./request"

const BASE_URL = "/api/v1/paper/file"

export const upload_file_action = `${BASE_URL}/upload`

export const FILE_STATUS = Object.freeze({
    StatusStarting     : "starting",
	StatusProcessing   : "processing",
	StatusUploaded     : "uploaded",
	StatusFailed       : "failed",
	StatusDeleteFailed : "delete failed",
	StatusDeleted      : "deleted",
})

export const PARSE_STATUS = Object.freeze({
    ParsePending    : "pending",
    ParseProcessing : "processing",
	ParseSuccess    : "success",
	ParseFailed     : "failed",
})

export const list_file_meta = wrap_api_func(async () => {
    const { data } = await request.get(`${BASE_URL}/meta/list`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: {
            metas: data.data ? data.data.metas : []
        }
    }
})

export const list_file_meta_by_parsestatus = wrap_api_func(async ({parse_status}) => {
    if (!Object.values(PARSE_STATUS).includes(parse_status)) {
        parse_status = "all"
    }
    const { data } = await request.get(`${BASE_URL}/meta/list?parse_status=${parse_status}`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: {
            metas: data.data ? data.data.metas : []
        }
    }
})

export const delete_file = wrap_api_func(async ({fileId}) => {
    const { data } = await request.post(`${BASE_URL}/delete/${fileId}`, null, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? data.meta : null
    }
})